package com.becca.cake.controller.couponRule;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.coupon.GoodsPriceSizeRuleModel;
import com.becca.cake.model.coupon.PartookCouponRuleModel;
import com.becca.cake.model.coupon.RuleGroupModel;
import com.becca.cake.model.coupon.SalesRuleGroupModel;
import com.becca.cake.model.coupon.SalesRuleModel;
import com.becca.cake.model.goods.GoodsTreeModel;
import com.becca.cake.model.users.UserCouponModel;
import com.becca.cake.model.users.UsersModel;
import com.becca.cake.service.couponRule.ICouponRuleService;
import com.becca.cake.service.users.IUserManagerService;
import com.becca.cake.util.JsonUtil;

/**
 * 分享红包规则controller
 * @copyright  尊星(上海)信息科技有限公司
 * @author yellow
 * @date 2016年6月7日 下午5:08:21
 */
@Controller
@RequestMapping("/couponRuleManager")
public class CouponRuleAction extends BaseAction {
    private final static Log    logger = LogFactory.getLog(CouponRuleAction.class);

    @Autowired
    private ICouponRuleService  iCouponRuleService;

    @Autowired
    private IUserManagerService iUserManagerService;

    /**
     * 查询红包规则列表
     * @param partookCouponRuleModel
     * @return
     */
    @RequestMapping(value = "/queryCouponRules", method = RequestMethod.POST)
    @ResponseBody
    public String queryCouponRules(PartookCouponRuleModel partookCouponRuleModel) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("partookCouponRuleModel", partookCouponRuleModel);
        map.put("pagingBean", page);
        try {
            // step1:获取数据
            List<PartookCouponRuleModel> list = this.iCouponRuleService.selectCouponRule(map);
            // step2:获取总条数
            Long count = this.iCouponRuleService.selectCouponRuleCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 修改红包规则是否可用
     * @param id
     * @param isEnable
     * @return
     */
    @RequestMapping(value = "/updateIsEnable", method = RequestMethod.POST)
    @ResponseBody
    public String updCouponRuleIsEnable(@RequestParam(value = "id", required = false) Integer id,
                                        @RequestParam(value = "isEnable", required = false) String isEnable) {
        try {
            //step1:请求参数
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("id", id);
            paramMap.put("isEnable", isEnable);
            paramMap.put("userName", this.getUserSeesion().getName());
            this.iCouponRuleService.updCouponRuleIsEnable(paramMap);
            return JsonUtil.returnJsonSuccess("删除红包规则成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据id查询红包规则对象
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/queryCouponRuleById", method = RequestMethod.POST)
    @ResponseBody
    public String queryCouponRuleById(@RequestParam(value = "id", required = false) Integer id) {
        try {
            PartookCouponRuleModel partookCouponRuleModel = this.iCouponRuleService
                .selectCouponRuleById(id);
            return JsonUtil.writeObjectFormJSON(partookCouponRuleModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 红包规则更改isEnable
     * @param partookCouponRuleModel
     * @return
     */
    @RequestMapping(value = "/updCouponRuleById", method = RequestMethod.POST)
    @ResponseBody
    public String updCouponRuleById(PartookCouponRuleModel partookCouponRuleModel) {
        try {
            this.iCouponRuleService.updateCouponRule(partookCouponRuleModel, this.getUserSeesion()
                .getTrueName());
            return JsonUtil.returnJsonSuccess("修改红包规则成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 查询优惠券列表
     * @param userMobileNo
     * @return
     */
    @RequestMapping(value = "/selectUserCoupons", method = RequestMethod.POST)
    @ResponseBody
    public String selectUserCoupons(UserCouponModel userCouponModel) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pagingBean", page);
        map.put("userCouponModel", userCouponModel);
        try {
            List<UserCouponModel> list = this.iCouponRuleService.selectUserCoupon(map);
            Long count = this.iCouponRuleService.selectUserCouponCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据手机查询用户
     * @param userMoblieNo
     * @return
     */
    @RequestMapping(value = "/selectUserByMobileNo", method = RequestMethod.POST)
    @ResponseBody
    public String selectUserByMobileNo(@RequestParam(value = "userMoblieNo", required = false) String userMoblieNo) {
        try {
            UsersModel usersModel = this.iUserManagerService.selectUserByMobileNo(userMoblieNo);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("success", true);
            map.put("data", usersModel);
            return JsonUtil.writeObjectJSON(map);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 赠送红包
     * @param userCouponModel
     * @return
     */
    @RequestMapping(value = "/insertUserCoupon", method = RequestMethod.POST)
    @ResponseBody
    public String insertUserCoupon(UserCouponModel userCouponModel,
                                   @RequestParam(value = "useDate", required = false) Integer useDate,
                                   @RequestParam(value = "isUseDate", required = false) Integer isUseDate) {
        Map<String, Object> map = new HashMap<String, Object>();
        userCouponModel.setCreatedName(this.getUserSeesion().getTrueName());
        map.put("useDate", useDate);
        map.put("isUseDate", isUseDate);
        map.put("userCouponModel", userCouponModel);
        try {
            this.iCouponRuleService.insertUserCoupon(map);
            return JsonUtil.returnJsonSuccess("红包赠送成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 赠送全部用户红包
     * @param userCouponModel
     * @return
     */
    @RequestMapping(value = "/insertAllUserCoupon", method = RequestMethod.POST)
    @ResponseBody
    public String insertAllUserCoupon(UserCouponModel userCouponModel,
                                      @RequestParam(value = "useDate", required = false) Integer useDate,
                                      @RequestParam(value = "isUseDate", required = false) Integer isUseDate) {
        Map<String, Object> map = new HashMap<String, Object>();
        userCouponModel.setCreatedName(this.getUserSeesion().getTrueName());
        map.put("useDate", useDate);
        map.put("isUseDate", isUseDate);
        map.put("userCouponModel", userCouponModel);
        try {
            this.iCouponRuleService.insertAllUserCoupon(map);
            return JsonUtil.returnJsonSuccess("红包赠送成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 查询规则组数据
     * @param goodsModel
     * @return
     */
    @RequestMapping(value = "/selectRuleGroup", method = RequestMethod.POST)
    @ResponseBody
    public String selectRuleGroup(RuleGroupModel ruleGroupModel,
                                  @RequestParam(value = "existall", required = false) String existall) {
        Map<String, Object> map = new HashMap<String, Object>();
        PagingBean page = getInitPagingBean();
        map.put("pagingBean", page);
        map.put("ruleGroupModel", ruleGroupModel);
        try {
            // step1:查询规则组数据
            List<RuleGroupModel> list = this.iCouponRuleService.selectRuleGroup(map);
            if ("true".equals(existall)) {
                RuleGroupModel s = new RuleGroupModel();
                s.setRuleGroupName("全部");
                list.add(s);
            }
            // step2:获取规则组总条数
            Long count = this.iCouponRuleService.selectRuleGroupCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据规则组ID查询规则组
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryRuleGroupById", method = RequestMethod.POST)
    @ResponseBody
    public String queryRuleGroupById(@RequestParam(value = "id", required = false) Integer id) {
        try {
            RuleGroupModel ruleGroupModel = this.iCouponRuleService.queryRuleGroupById(id);
            return JsonUtil.writeObjectFormJSON(ruleGroupModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 添加或修改规则组信息
     * @param ruleGroupModel
     * @return
     */
    @RequestMapping(value = "/addOrUpdRuleGroup", method = RequestMethod.POST)
    @ResponseBody
    public String addOrUpdRuleGroup(RuleGroupModel ruleGroupModel) {
        try {
            this.iCouponRuleService.insertOrUpdateRuleGroup(ruleGroupModel, this.getUserSeesion()
                .getTrueName());
            return JsonUtil.returnJsonSuccess("保存规则组信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 规则管理查询列表
     * @param salesRuleModel
     * @return
     */
    @RequestMapping(value = "/querySalesRule", method = RequestMethod.POST)
    @ResponseBody
    public String querySalesRule(SalesRuleModel salesRuleModel) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("salesRuleModel", salesRuleModel);
        map.put("pagingBean", page);
        try {
            // step1:获取数据
            List<SalesRuleModel> list = this.iCouponRuleService.selectSalesRule(map);
            // step2:获取总条数
            Long count = this.iCouponRuleService.selectSalesRuleCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 规则组添加规则
     * @param salesRuleIds
     * @return
     */
    @RequestMapping(value = "/updSalesRuleGroup", method = RequestMethod.POST)
    @ResponseBody
    public String updSalesRuleGroup(@RequestParam(value = "ruleGroupId", required = false) Integer ruleGroupId,
                                    @RequestParam(value = "salesRuleIds", required = false) String salesRuleIds) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ruleGroupId", ruleGroupId);
        map.put("salesRuleIds", salesRuleIds.split(","));
        try {
            this.iCouponRuleService.updSalesRuleGroup(map);
            return JsonUtil.returnJsonSuccess("添加规则成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据规则组id查询规则集合
     * @param ruleGroupId
     * @return
     */
    @RequestMapping(value = "/querySalesRuleGroupById", method = RequestMethod.POST)
    @ResponseBody
    public String querySalesRuleGroupById(@RequestParam(value = "ruleGroupId", required = false) Integer ruleGroupId) {
        try {
            List<SalesRuleGroupModel> list = this.iCouponRuleService
                .querySalesRuleGroupById(ruleGroupId);
            return JsonUtil.writeObjectNoPageJSON(list);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 添加或修改规则信息
     * @param salesRuleModel
     * @return
     */
    @RequestMapping(value = "/insertOrUpdateSalesRule", method = RequestMethod.POST)
    @ResponseBody
    public String insertOrUpdateSalesRule(SalesRuleModel salesRuleModel) {
        try {
            this.iCouponRuleService.insertOrUpdateSalesRule(salesRuleModel, this.getUserSeesion()
                .getTrueName());
            return JsonUtil.returnJsonSuccess("保存规则信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据规则ID查询规则对象
     * @param id
     * @return
     */
    @RequestMapping(value = "/querySalesRuleById", method = RequestMethod.POST)
    @ResponseBody
    public String querySalesRuleById(@RequestParam(value = "id", required = false) Integer id) {
        try {
            SalesRuleModel salesRuleModel = this.iCouponRuleService.querySalesRuleById(id);
            return JsonUtil.writeObjectFormJSON(salesRuleModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 修改是否可用/不可用
     * @param id
     * @param isEnable
     * @return
     */
    @RequestMapping(value = "/updIsEnableById", method = RequestMethod.POST)
    @ResponseBody
    public String updIsEnableById(@RequestParam(value = "id", required = false) String id,
                                  @RequestParam(value = "isEnable", required = false) String isEnable) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("id", id);
        paramMap.put("isEnable", isEnable);
        paramMap.put("userName", this.getUserSeesion().getTrueName());
        try {
            this.iCouponRuleService.updIsEnableById(paramMap);
            return JsonUtil.returnJsonSuccess("修改成功！");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }

    }

    /**
     * 查询商品规格tree
     * @param goodsStatus
     * @return
     */
    @RequestMapping(value = "/selectGoodsTree", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodsTree(@RequestParam(value = "goodsStatus", required = false) Integer goodsStatus,
                                  @RequestParam(value = "salesRuleId", required = false) Integer salesRuleId,
                                  @RequestParam(value = "isDelete", required = false) Integer isDelete) {
        try {
            // step1:获取数据
            List<GoodsTreeModel> goodsTreeList = this.iCouponRuleService.selectGoodsTree(
                goodsStatus, isDelete);
            return JsonUtil.writeObjectJSON(goodsTreeList);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 修改商品规格
     * @param ruleId
     * @param rights
     * @return
     */
    @RequestMapping(value = "/updateGoodsRule", method = RequestMethod.POST)
    @ResponseBody
    public String updateGoodsRule(@RequestParam(value = "salesRuleId", required = false) Integer salesRuleId,
                                  @RequestParam(value = "goodsPriceSizeIds", required = false) String goodsPriceSizeIds) {
        try {
            this.iCouponRuleService.updateGoodsRule(salesRuleId, goodsPriceSizeIds.split(","));
            return JsonUtil.returnJsonSuccess("添加商品规格成功！");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 查询需要选中的商品
     * @param salesRuleId
     * @return
     */
    @RequestMapping(value = "/selectGoodsBySalesRuleId", method = RequestMethod.POST)
    @ResponseBody
    public String selectGoodsBySalesRuleId(@RequestParam(value = "salesRuleId", required = false) Integer salesRuleId) {
        try {
            List<GoodsPriceSizeRuleModel> list = this.iCouponRuleService
                .selectGoodsBySalesRuleId(salesRuleId);
            return JsonUtil.writeObjectJSON(list);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
}
